package com.zeroup.auto.assists.step.dy;

import android.graphics.Path;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.annotation.NonNull;

import com.ven.assists.stepper.StepData;
import com.zeroup.auto.assists.DouYinBaseStep;
import com.zeroup.auto.assists.next.dy.YhStepBeanData;
import com.zeroup.auto.utils.AssistsUtils;
import com.ven.assists.Assists;
import com.ven.assists.stepper.StepCollector;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 养号  循环看关键内容的作品
 *  是关键字内容的  可以关注  点赞
 *
 */
public class YhStep extends DouYinBaseStep {


    private String remarkId = "";

    List<String> keyWords = new ArrayList<>();



    @Override
    public void onImpl(@NonNull StepCollector stepCollector) {
        stepCollector.next(1,false,(i,c)->{
            Object serviceData = AssistsUtils.getServiceData(i.getData());
            if(serviceData==null){
                return AssistsUtils.nextStep(i.getData());
            }
            if(serviceData instanceof YhStepBeanData){
                try {
                    YhStepBeanData stepBeanData = (YhStepBeanData)serviceData;
                    keyWords = new ArrayList<>(Arrays.asList(stepBeanData.getKeyWord().split(",|\\R")));
                    if(stepBeanData.getEndTime()!=null){
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date = formatter.parse(stepBeanData.getEndTime());
                        Date now = new Date();
                        while (now.before(date)){
                            Path mPath=new Path();//线性的path代表手势路径,点代表按下,封闭的没用
                            mPath.moveTo(500, 1000);
                            mPath.lineTo(500, 400);
//                            Assists.gesture(mPath,0,300);
                            now = new Date();
                            if(isKeyWord()){
                                if(stepBeanData.getIsFllow()==1){//关注
                                    fllow();
                                }
                                if(stepBeanData.getIsLike()==1){//点赞
                                    like();
                                }
                                try {
                                    Thread.sleep(stepBeanData.getSleepValue());
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                            }else {
                                try {
                                    Thread.sleep(800);
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                        }
                    }
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
            return AssistsUtils.nextStep(i.getData());
        });
    }
    private boolean isKeyWord(){
        List<AccessibilityNodeInfo> infoList = Assists.INSTANCE.findById(remarkId);
        if(infoList.isEmpty()){
            return false;
        }
        for (AccessibilityNodeInfo info:infoList){
            String kjName =  info.getText().toString();
            return keyWords.stream()
                    .anyMatch(kjName::contains);
        }
        return false;
    }


}
